package hu.myai.indicators.tick;

import hu.myai.api.indicators.AbstractIndicator;
import hu.myai.model.IndicatorVO;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Is for the % of the volume change of a tick compares to previous tick. It
 * operates the last 3 tick.
 * 
 * @author bozbalint
 * 
 * */
public class TickVolumeChangeIndicator extends AbstractIndicator {

    Double prevValue = null;

    /**
     * The object used when logging debug,errors,warnings and info.
     * */
    private final Logger log = LoggerFactory.getLogger(this.getClass());

    /**
     * Constructor
     */
    public TickVolumeChangeIndicator() {
        super(null);
    }

    @Override
    public void init() {
    }

    @Override
    protected IndicatorVO indicatorCalculation() {

        // no calculation if no volume
        if (lastTick.getVolume() == 0 || tickHistory.size() < 4) {
            return null;
        }

        // the average of the last 2 tick
        double avgVolume = (tickHistory.get(1).getVolume() + tickHistory.get(2).getVolume() + tickHistory.get(3)
                .getVolume()) / 3;

        IndicatorVO indicator = new IndicatorVO();
        indicator.setValue(lastTick.getVolume() / avgVolume);
        indicator.setDate(lastTick.getTime());

        if (prevValue == null) {
            indicator.setChanged(true);
        } else {
            indicator.setChanged(prevValue != indicator.getValue());
        }

        prevValue = indicator.getValue();

        log.debug(indicator.toString());
        return indicator;
    }
}
